﻿using System;
using System.Windows.Forms;
using ExpressionEvalSample.Domain;
using SimpleExpressionEvaluator;

namespace ExpressionEvalSample
{
    public partial class ExpressionEvalForm : Form
    {
        public ExpressionEvaluator Eval = new ExpressionEvaluator();
        public Model Model = new Model();

        public ExpressionEvalForm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            Model.GenerateTestData(5, 3);

            RefreshModelTree(Model);

            btnEvaluate.PerformClick();
        }

        protected void RefreshModelTree(Model model)
        {
            treeModelView.Nodes.Clear();
            treeModelView.Nodes.Add(new TreeNode("Date: " + model.Date));
            
            var inAir = new TreeNode("PlanesInTheAir");
            foreach (Airplane plane in model.PlanesInTheAir)
            {
                inAir.Nodes.Add(CreateAirplaneNode(plane));
            }
            treeModelView.Nodes.Add(inAir);

            var onGround = new TreeNode("PlanesOnTheGround");
            foreach (Airplane plane in model.PlanesOnTheGround)
            {
                onGround.Nodes.Add(CreateAirplaneNode(plane));
            }
            treeModelView.Nodes.Add(onGround);

            inAir.Expand();
            onGround.Expand();
        }

        protected static TreeNode CreateAirplaneNode(Airplane plane)
        {
            var pn = new TreeNode(plane.Type + " : " + plane.TailNumber);
            pn.Nodes.Add(new TreeNode("TailNumber:" + plane.TailNumber));
            pn.Nodes.Add(new TreeNode("Type: " + plane.Type));
            pn.Nodes.Add(new TreeNode("CurrentSpeed: " + plane.CurrentSpeed));
            pn.Nodes.Add(new TreeNode("Altitude: " + plane.Altitude));
            pn.Nodes.Add(new TreeNode("MaxSpeed: " + plane.MaxSpeed));

            var pilotNode = CreatePersonNode(plane.Pilot);
            pilotNode.Text = "Pilot";
            pn.Nodes.Add(pilotNode);

            var pass = new TreeNode("Passengers");
            foreach (Passenger passenger in plane.Passengers)
            {
                pass.Nodes.Add(CreatePassengerNode(passenger));
            }
            pn.Nodes.Add(pass);
            return pn;
        }

        protected static TreeNode CreatePersonNode(Person person)
        {
            var pn = new TreeNode(person.Name);
            pn.Nodes.Add("Name: " + person.Name);
            pn.Nodes.Add("DateOfBirth: " + person.DateOfBirth.ToString());
            pn.Nodes.Add("AgeInYears:" + person.AgeInYears);
            return pn;
        }

        protected static TreeNode CreatePassengerNode(Passenger passenger)
        {
            TreeNode pn = CreatePersonNode(passenger);
            pn.Text = pn.Text + " @ " + passenger.SeatAssignment;
            pn.Nodes.Add("SeatAssignment: " + passenger.SeatAssignment);
            return pn;
        }

        private void btnEvaluate_Click(object sender, System.EventArgs e)
        {
            try
            {
                object output = Eval.EvaluateExpression<object>(txtExpression.Text, Model);
                if (output != null)
                    txtOutput.Text = output.ToString();
                else
                    txtOutput.Text = "The expression returned null.";
            }
            catch (Exception ex)
            {
                txtOutput.Text = ex.Message;
            }
        }
    }
}
